﻿/*
 * Rappelz: Endless Odyssey - The first free open source Rappelz server emulator
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rappelz.GameServer.Network;
using System.Data.Common;
using System.Data;
using Rappelz.GameServer.Network.GameClient;

namespace Rappelz.GameServer.Database
{
    /// <summary>
    /// DBLoader_FieldPropData
    /// </summary>
    public class DBLoader_FieldPropData : IDBProc
    {
        GameDBManager db;
        public BaseObject m_CBObject = null;

        public DBLoader_FieldPropData(GameDBManager dbManager)
        {
            db = dbManager;
        }

        public void SetCB(BaseObject obj)
        {
            m_CBObject = obj;
        }

        public void RunCB()
        {
            if (m_CBObject != null)
                m_CBObject.onEndQuery();
        }

        public bool onProcess()
        {
            TerrainPropInfo propInfo = new TerrainPropInfo();

            if (!propInfo.Initialize("TerrainPropInfo.cfg"))
            {
                Globals.Log.Fatal("TerrainPropInfo.cfg read error ! ");
                return false;
            }

            GameContent.s_fTileSize = GameContent.g_SeamlessWorldInfo.m_fTileLength;

            int y = 0;
            float fMapLength = GameContent.g_SeamlessWorldInfo.m_nSegmentCountPerMap * GameContent.g_SeamlessWorldInfo.m_fTileLength * GameContent.g_SeamlessWorldInfo.m_nTileCountPerSegment;
            GameContent.g_fMapLength = fMapLength;
            for (float fAttrLen = GameContent.g_SeamlessWorldInfo.m_fTileLength * 0.125f;
                  y < GameContent.g_SeamlessWorldInfo.m_sizMapCount.height;
                  ++y )
            {
                for (int i = 0; i < GameContent.g_SeamlessWorldInfo.m_sizMapCount.width; ++i)
                {
                    string strString = GameContent.g_SeamlessWorldInfo.GetFieldPropFileName(i, y);

                    if (strString.Length != 0)
                    {
                        strString = ResourceManager.GetFullPathname(strString);
                        LoadFieldPropFile(strString, i, y, fAttrLen, fMapLength);
                    }
                }
            }
            Globals.Log.Info("FieldPropData Loading Complete..");
            return true;
        }

        private void LoadFieldPropFile(string szFileName, int x, int y, float fAttrLen, float fMapLength)
        {
            if (!System.IO.File.Exists(szFileName))
            {
                Globals.Log.Debug("File Not Found: {0}", szFileName);
                return;
            }

            try
            {
                System.IO.FileStream _FileStream = new System.IO.FileStream(szFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                System.IO.BinaryReader br = new System.IO.BinaryReader(_FileStream);
                String szSign = new String(br.ReadChars(18));
                Int32 dwVersion = br.ReadInt32();

                int total_entries = br.ReadInt32();
                float rx = x * fMapLength;
                float ry = y * fMapLength;

                for(int i = 0; i < total_entries; ++i)
                {
                    GameContent.FieldPropRespawnInfo sr = new GameContent.FieldPropRespawnInfo();
                    sr.nPropId = br.ReadInt32();
                    sr.x = br.ReadSingle() + rx;
                    sr.y = br.ReadSingle() + ry;
                    sr.fZOffset = br.ReadSingle();
                    sr.fRotateX = br.ReadSingle();
                    sr.fRotateY = br.ReadSingle();
                    sr.fRotateZ = br.ReadSingle();
                    sr.fScaleX = br.ReadSingle();
                    sr.fScaleY = br.ReadSingle();
                    sr.fScaleZ = br.ReadSingle();
                    sr.layer = 0;
                    sr.bOnce = false;
                    br.ReadBytes(9);

                    FieldPropManager.Instance.RegisterFieldProp(sr);
                }
                _FileStream.Close();
                _FileStream.Dispose();
                _FileStream = null;
                br.Close();
            }
            catch (System.IO.FileNotFoundException)
            {
                Globals.Log.Error("File Not Found: {0}", szFileName);
            }
            catch (System.Exception ex)
            {
                Globals.Log.Error("Error Loading FieldPropData File: {0} - {1}", szFileName, ex);
            }
        }
    }

}

